//
//  JZHMyCollectViewController.m
//  极致荟
//
//  Created by hetao on 14-5-14.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "JZHMyCollectViewController.h"
#import "JZHMyCollectTableViewCell.h"
#import "JZHCommodity.h"
#import "JZHActivities.h"
#import "JZHNoProductViewController.h"
#import "JZHBlackCardDetailViewController.h"
@interface JZHMyCollectViewController ()
{
    NSMutableArray *dataarr;
}
@property (weak, nonatomic) IBOutlet UITableView *table;
@end

@implementation JZHMyCollectViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self httpLoadData];

}
- (void)viewDidLoad
{
    [super viewDidLoad];
    dataarr=[[NSMutableArray alloc]init];
    // Do any additional setup after loading the view from its nib.
    if(OSVersionIsAtLeastiOS7>=7.0)
    {
        self.automaticallyAdjustsScrollViewInsets=NO;
    }
    self.navigationItem.title=@"我的收藏";
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 12, 20)];
    [leftButton addTarget:self action:@selector(leftDrawerButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    [leftButton setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    
}

- (void)leftDrawerButtonPress:(UIButton *)but
{
    [UINavigationController Pop:self];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark ---tableview代理---
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     NSLog(@"+++%d",dataarr.count);
    return dataarr.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    static NSString *str=@"adf";
    JZHMyCollectTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:str];
    if(!cell)
    {
        cell=[[NSBundle mainBundle] loadNibNamed:@"JZHMyCollectTableViewCell" owner:self options:nil][0];
        
    }
    cell.deleteliset=^(){
        [self deletelist:dataarr[indexPath.row]];
    };
    [cell addCellData:dataarr[indexPath.row]];
   
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if([dataarr[indexPath.row]  isKindOfClass:[ JZHActivities class]])
    {
        [self.navigationController pushViewController:[[JZHBlackCardDetailViewController alloc] initCommodity:dataarr[indexPath.row]] animated:YES];
    }else
    {
        [self.navigationController pushViewController:[[JZHNoProductViewController alloc] initcom:dataarr[indexPath.row]] animated:YES];
    }
}

- (void)deletelist:(id)datad
{
    int fid=0;
    if([datad isKindOfClass:[JZHCommodity class]])
    {
        JZHCommodity *jzh=datad;
        fid=jzh.scId;
    }else
    {
        JZHActivities *jzh=datad;
        fid=jzh.scId;
    }
    NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithInt:fid],@"favoriteId", nil];
    [HttpRequest httpRequestPost:JZHHTTPURL(@"myInformation/delFavorite") parameters:dic block:^(id data, HttpReturnCode rerurncode) {
        if(rerurncode==0)
        {
            [dataarr removeObject:datad];
            [self.table reloadData];
        }
    }];
}
- (void)httpLoadData
{
    NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithInt:[User defuceUser].userId],@"userId", nil];
    [HttpRequest httpRequestPost:JZHHTTPURL(@"myInformation/myFavorite") parameters:dic block:^(id data, HttpReturnCode rerurncode) {
        if(rerurncode==0)
        {
            NSLog(@"%@",data);
            [dataarr removeAllObjects];
            for(NSDictionary *datadic in [[data objectForKey:@"data"] objectForKey:@"list"])
            {
                if([[datadic objectForKey:@"typeName"] isEqualToString:@"club"])//活动
                {
                    JZHActivities *act=[[JZHActivities alloc]init];
                    act.acttile=[datadic objectForKey:@"title"];
                    act.actdate=[NSDate dateWithTimeIntervalSince1970:[[datadic objectForKey:@"addTime"] longLongValue]];
                    act.actId=[[datadic objectForKey:@"favoriteId"] intValue];
                    act.actimage=[[User defuceUser].pathHead stringByAppendingString:[datadic objectForKey:@"images"]];
                    act.scId=[[datadic objectForKey:@"id"] intValue];
                    [dataarr addObject:act];
                    
                    continue;
                }else//商品
                {
                    JZHCommodity *com=[[JZHCommodity alloc]init];
                    if([datadic objectForKey:@"images"]!=[NSNull null])
                    {
                     com.headimage=[[User defuceUser].pathHead stringByAppendingString:[datadic objectForKey:@"images"]];
                    }
                   
                    com.name=[datadic objectForKey:@"title"];
                    if([datadic objectForKey:@"favoriteId"]!=[NSNull null])
                    {
                        com.comid=[[datadic objectForKey:@"favoriteId"] intValue];
                    }
                    
                    com.bigentime=[[datadic objectForKey:@"addTime"] floatValue];
                    com.scId=[[datadic objectForKey:@"id"] intValue];
                    [dataarr addObject:com];

                }
            }
            
            [self.table reloadData];
            
        }
    }];
}
@end
